﻿#define FakeLogin

using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using System.IO;
using Dofull.Globalization;
using System.Collections.Generic;


namespace VPNNew
{
    public abstract class PageBase : System.Web.UI.Page
    {
        #region 实现国际化
        protected override void Render(HtmlTextWriter writer)
        {
            if (ScriptManager.GetCurrent(this) != null && ScriptManager.GetCurrent(this).IsInAsyncPostBack)
            {
                StringBuilder sb = new StringBuilder();
                StringWriter sw = new StringWriter(sb);
                HtmlTextWriter htw = new HtmlTextWriter(sw);

                GlobalizeWriter gw = new GlobalizeWriter(htw);
                base.Render(gw);

                if (gw.LengthChange != 0)
                {
                    string s = sb.ToString();
                    List<updatePanelString> ups;
                    string[] ss = s.Split("|".ToCharArray());
                    ups = ss.Select(bs => new updatePanelString() { BlockString = bs }).ToList();

                    var gdString = (from gs in ups
                                    where (gs.BlockString == "updatePanel" && (ups[ups.IndexOf(gs) + 4].BlockString == "updatePanel" || ups[ups.IndexOf(gs) + 4].BlockString == "hiddenField"))
                                    || gs.BlockString == "expando" || (gs.BlockString == "pageTitle" && (ups[ups.IndexOf(gs) + 1].BlockString == string.Empty))
                                    || gs.BlockString == "scriptBlock"
                                    select new { Index = ups.IndexOf(gs) - 1, Value = ups[ups.IndexOf(gs) + 2].BlockString.Length }).ToArray();

                    foreach (var gd in gdString)
                    {
                        ss[gd.Index] = gd.Value.ToString();
                    }

                    writer.Write(string.Join("|", ss.ToArray()));
                }
                else
                {
                    writer.Write(sb.ToString());
                }
            }
            else
            {
                base.Render(new GlobalizeWriter(writer));
            }
        }

        private class updatePanelString
        {
            public string BlockString
            {
                get;
                set;
            }
        }

        protected string GRString(string tag)
        {
            return GR.GetString(tag);
        }
        #endregion

        #region 导航
        public abstract string PageCode
        {
            get;
        }
        #endregion

        #region 伪装登录
#if FakeLogin
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            HttpContext.Current.User = new System.Security.Principal.GenericPrincipal(new System.Security.Principal.GenericIdentity("TestUser1"), new string[] { "VPNUSer" });
        }
#endif
        #endregion
    }
}
